package edu.study.user.controller;

import edu.study.api.common.dto.BaseRespDto;
import edu.study.api.common.dto.RespDto;
import edu.study.common.util.cache.Lock;
import edu.study.common.util.cache.LockExecuteCallback;
import edu.study.user.api.UserApi;
import edu.study.user.api.dto.UserDetailDto;
import edu.study.user.api.dto.UserDto;
import edu.study.user.api.req.user.AddUserReq;
import edu.study.user.api.req.user.LoginReq;
import edu.study.user.api.req.user.ModifyPasswordReq;
import edu.study.user.api.req.user.QueryUserListReq;
import edu.study.user.api.req.user.QueryUserReq;
import edu.study.user.service.UserService;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserApiImpl implements UserApi {

	@Autowired
    private Lock lock;

    @Autowired
    private UserService userService;

    @Override
    public BaseRespDto addUser(@RequestBody AddUserReq req) {
        BaseRespDto resp = lock.lockAndExecute(req.getLoginName(), req.get_requestId(), new LockExecuteCallback() {
            @Override
            public BaseRespDto doBusiness() {
                BaseRespDto resp1 = userService.addUser(req);
                return resp1;
            }
        });
        return resp;
    }
    
    @Override
    public RespDto<UserDetailDto> login(@RequestBody LoginReq req) {
        RespDto<UserDetailDto> resp = userService.login(req);
        return resp;
    }
    
    @Override
    public BaseRespDto modifyPassword(@RequestBody ModifyPasswordReq req) {
    	BaseRespDto resp = userService.modifyPassword(req);
        return resp;
    }
    
    @Override
	public RespDto<UserDto> queryUser(@RequestBody QueryUserReq req) {
    	RespDto<UserDto> resp = userService.queryUser(req);
		return resp;
	}
    
    @Override
	public RespDto<List<UserDto>> queryUserList(@RequestBody QueryUserListReq req) {
    	RespDto<List<UserDto>> resp = userService.queryUserList(req);
		return resp;
	}
}
